package main

import "fmt"

func main() {
	var nums []int // 此时切片 nums 初值为 nil，因为没有初始化，未“绑定”底层数组
	fmt.Println("nums == nil ?", nums == nil)
	fmt.Printf("%p\n", nums)
	fmt.Println(len(nums), cap(nums)) // len = 0, cap = 0

	// nums 是 nil 时使用 for range 会发生什么？
	for idx, val := range nums { // 如果 slice 是 nil， for range 会跳过，不会报错
		fmt.Println(idx, val)
	}

	nums = append(nums, 11) // len = 1, cap = 1
	fmt.Println(len(nums), cap(nums))

	nums = append(nums, 12) // len = 2, cap = 2
	fmt.Println(len(nums), cap(nums))

	nums = append(nums, 13) // len = 3, cap = 4
	fmt.Println(len(nums), cap(nums))

	nums = append(nums, 14) // len = 4, cap = 4
	fmt.Println(len(nums), cap(nums))

	nums = append(nums, 15) // len = 5, cap = 8
	fmt.Println(len(nums), cap(nums))

	var s = []int{}
	fmt.Println("s == nil ?", s == nil)
	fmt.Printf("%p\n", s)
	fmt.Println(len(s), cap(s))
}
